package com.example.dqnm.service.impl;

import com.example.dqnm.mapper.CommentsDao;
import com.example.dqnm.mapper.OrdersDao;
import com.example.dqnm.pojo.Comments;
import com.example.dqnm.pojo.CommentsExample;
import com.example.dqnm.pojo.CommentsQuery;
import com.example.dqnm.pojo.Orders;
import com.example.dqnm.service.commentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

@Service
public class commentServiceImpl implements commentService {
    @Autowired
    private CommentsDao commentsDao;

    @Autowired
    private OrdersDao ordersDao;

    @Override
    public Boolean addComments(Comments comments) {
        // 默认点赞数为0
        comments.setLikesCount(0);
        comments.setCreateTime(new Date());

        int insert = commentsDao.insert(comments);
        boolean flag =  insert > 0;
        // 如果新增成功，则需要在订单标记已经评价
        if (flag) {
            Orders orders = new Orders();
            orders.setId(comments.getOrderId());
            // 标记
            orders.setIscomment(1);

            ordersDao.updateByPrimaryKeySelective(orders);
        }

        return flag;
    }

    @Override
    public List<Comments> findCommentsByQuery(CommentsQuery commentsQuery) {
        CommentsExample commentsExample = new CommentsExample();
        CommentsExample.Criteria criteria = commentsExample.createCriteria();

        if (commentsQuery.getUserId() != -1) {
            criteria.andUserIdEqualTo(commentsQuery.getUserId());
        }
        if (commentsQuery.getOrderId() != -1) {
            criteria.andOrderIdEqualTo(commentsQuery.getOrderId());
        }
        if (commentsQuery.getStoreId() != -1) {
            criteria.andStoreIdEqualTo(commentsQuery.getStoreId());
        }

        return commentsDao.selectByExample(commentsExample);
    }
}
